"""
Case Type   : security-EqualityQuery
Case Name   : 重复创建主密钥对象
Create At   : 2022/08/23
Owner       : @zou_jialiang050
Description :
    1.gsql客户端-C连接数据库,创建cmk
    2.删除第一步创建的cmk
    3.创建重名cmk
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
    2024/11/22 modify by @peilinqian: 定位偶现问题单IATFYV，增加调试打印参数
    2023/09/07 modify by @peilinqian: 研发代码变更加密算法变更为rsa_3072，优化用例
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Auditing(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.userNode = Node('PrimaryDbUser')
        self.database = self.userNode.db_name
        self.com = Common()
        self.cmk = 'cmk_security_qualityquery_0059'
        self.key_path = 'keypath_security_qualityquery_0059_1'
        self.key_path2 = 'keypath_security_qualityquery_0059_2'
        self.localkms = os.path.join(macro.DB_INSTANCE_PATH, '..', 'app',
                                     'etc', 'localkms')

    def test_security(self):
        text = '-----step1:gsql客户端-C连接数据库,创建cmk;expect:成功-----'
        self.logger.info(text)
        text = '-----配置$GAUSSHOME/etc/localkms路径;expect:成功-----'
        self.logger.info(text)
        mkdir_cmd = f'if [ ! -d "{self.localkms}" ];' \
            f'then mkdir {self.localkms};fi'
        self.logger.info(mkdir_cmd)
        self.userNode.sh(mkdir_cmd).result()
        sql_cmd = f'drop client master key if exists {self.cmk};' \
            f'create client master key {self.cmk} with ' \
            f'(key_store = localkms, key_path = "{self.key_path}", ' \
            f'algorithm = rsa_3072);'
        self.logger.info(sql_cmd)
        msg = self.sh_primy.execut_db_sql(sql_cmd, sql_type='-3 -C')
        self.logger.info(msg)
        self.assertIn(self.constant.create_cmk_success, msg, text +
                      '执行失败')

        text = '-----step2:删除第一步创建的cmk;expect:成功-----'
        self.logger.info(text)
        sql_cmd = f'drop client master key {self.cmk};'
        self.logger.info(sql_cmd)
        result = self.sh_primy.execut_db_sql(sql_cmd, sql_type='-3 -C')
        self.logger.info(result)
        self.assertIn(self.constant.drop_cmk_success, result, text +
                      '执行失败')

        text = '-----step3:创建重名cmk;expect:成功-----'
        self.logger.info(text)
        sql_cmd = f'create client master key {self.cmk} with ' \
            f'(key_store = localkms, key_path = "{self.key_path}", ' \
            f'algorithm = rsa_3072);'
        self.logger.info(sql_cmd)
        msg = self.sh_primy.execut_db_sql(sql_cmd, sql_type='-3 -C')
        self.logger.info(msg)
        self.assertIn(self.constant.create_cmk_success, msg, text + '执行失败')

    def tearDown(self):
        text = '-----step4:清理环境;expect:成功-----'
        self.logger.info(text)
        sql_cmd = f'drop client master key if exists {self.cmk};'
        self.logger.info(sql_cmd)
        result = self.sh_primy.execut_db_sql(sql_cmd, sql_type='-3 -C')
        self.logger.info(result)

        clean_cmd = f"rm -rf {self.localkms};"
        self.logger.info(clean_cmd)
        clean_msg = self.userNode.sh(clean_cmd).result()
        self.logger.info(clean_msg)
        self.assertIn(self.constant.drop_cmk_success, result, text +
                      '执行失败')
        self.assertEqual("", clean_msg, text + '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
